Fix infinite loop in symbolWalker during extract function refactoring#62907
Open
Dino0204 wants to merge 6 commits intomicrosoft:mainfrom
Open
Fix infinite loop in symbolWalker during extract function refactoring#62907Dino0204 wants to merge 6 commits intomicrosoft:mainfrom
Dino0204 wants to merge 6 commits intomicrosoft:mainfrom
Conversation
Author
|
@microsoft-github-policy-service agree |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a stack overflow crash in the TypeScript language server that occurs during refactoring operations on code with deeply recursive generic types. The fix adds a recursion depth limit to the symbolWalker traversal logic to complement the existing cycle detection mechanism.
Key changes:
- Added depth tracking (
levelcounter andmaxRecursionDepthconstant) tovisitTypefunction in symbolWalker - Wrapped type visiting logic in try-finally to ensure proper depth counter cleanup
- Added a fourslash test to verify the fix prevents crashes
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/compiler/symbolWalker.ts | Adds recursion depth tracking with a limit of 100 levels to prevent stack overflow when traversing deeply nested recursive types |
| tests/cases/fourslash/extractFunctionSymbolWalkerInfiniteLoop.ts | Adds regression test for the symbolWalker infinite loop fix |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #62471
Description
This PR fixes a
RangeError: Maximum call stack size exceededcrash in the TypeScript Server (VS Code Extension) that occurs when invoking "Refactor..." actions on code involving recursive generic types.The issue was caused by
symbolWalkerentering an infinite loop when traversing recursive types (e.g., a type referencing itself in a property). While the walker already had cycle detection viavisitedTypesandvisitedSymbols, it lacked a depth limit. This allowed cases where the recursion depth exceeded the JavaScript call stack limit before the cycle detection could take effect.Reproduction
The following code triggers the crash when trying to use "Refactor..." (e.g., Extract Function) on the selection:
Behavior:
RangeError: Maximum call stack size exceeded.*Implementation Details
I modified
src/compiler/symbolWalker.tsto add recursion depth tracking in thevisitTypefunction.Key Changes:
levelcounter and amaxRecursionDepthconstant (set to 100) to track the current recursion depth.level > maxRecursionDepth, preventing the stack overflow.try-finallyblock to ensure thelevelcounter is properly decremented even on early returns or errors.Why this approach (Defense-in-Depth)?
This works in conjunction with the existing cycle detection (
visitedTypes) to provide a robust safeguard:visitedTypesarray prevents infinite loops on direct cycles.Test Plan
tests/cases/fourslash/extractFunctionSymbolWalkerInfiniteLoop.ts.verify.not.refactorAvailable("Extract function")on the recursive type pattern shown above.hereby runtestslocally.Baseline Changes
symbolWalker.